package com.heiye.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heiye.common.Code;
import com.heiye.pojo.StudentInfo;
import com.heiye.service.StudentInfoService;
import com.heiye.vo.StudentInfoVO;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
public class StuInfoController {

    @Autowired
    private Code code;
    @Autowired
    private StudentInfoService studentInfoService;


    /**
     * @param jsonObject
     * @return 0:该学生已经存在;1:注册成功
     */
    @PostMapping("/stuInfo/stuRegister")
    @ResponseBody
    public int stuRegister(@RequestBody JSONObject jsonObject) {
        String stuName = jsonObject.getString("stuName");
        String stuId = jsonObject.getString("stuId");
        String stuSex = jsonObject.getString("stuSex");
        String stuPassword = jsonObject.getString("stuPassword");

        //判断学号是否存在
        QueryWrapper<StudentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id", stuId);

        if (studentInfoService.getOne(queryWrapper) == null) {
            //否则进行注册
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setStuId(stuId);
            studentInfo.setStuName(stuName);
            studentInfo.setStuSex(stuSex);
            studentInfo.setStuPassword(stuPassword);
            studentInfoService.save(studentInfo);
            return 1;
        } else {
            //如果存在，告诉前端已经存在
            return 0;
        }
    }


    /**
     * 修改学生的密码(忘记密码)
     *
     * @param jsonObject
     * @return 0:学生不存在  -1:身份验证错误   1:修改成功
     */
    @PostMapping("/stuInfo/updateStuPassword")
    public int updateStuPassword(@RequestBody JSONObject jsonObject) {
        String stuName = jsonObject.getString("stuName");
        String stuId = jsonObject.getString("stuId");
        String NstuPassword = jsonObject.getString("NstuPassword");
        System.out.println(stuId);

        QueryWrapper<StudentInfo> queryWrapper1 = new QueryWrapper<>();
        //根据学号判断用户是否存在
        queryWrapper1.eq("stu_id", stuId);
        //不存在 返回0
        if (studentInfoService.getOne(queryWrapper1) == null) {
            return 0;
        }

        //判断学生信息是否正确，通过比较姓名
        QueryWrapper<StudentInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("stu_id", stuId);
        queryWrapper2.eq("stu_name", stuName);
        if (studentInfoService.getOne(queryWrapper2) == null) {
            //姓名不对  信息验证错误
            return -1;
        }

        //修改密码
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setStuPassword(NstuPassword);
        QueryWrapper<StudentInfo> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("stu_id", stuId);
        studentInfoService.update(studentInfo, queryWrapper3);
        return 1;
    }

    //根据学号获取学生的信息
    @GetMapping("/selectStudentInfoByStuNumber")
    public StudentInfoVO selectStudentInfoByStuNumber(@RequestParam("stuNumber") String stuNumber) {
        return studentInfoService.selectStudentInfoByStuNumber(stuNumber);
    }

}
